package com.lz.football.module.compete.item
{
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.CompeteEvent;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.goods.vo.GoodsVo;
	import com.lz.football.module.base.IComponent;
	import com.lz.football.module.compete.SingleChaglleView;
	
	import common.BaseView.GoodItem;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	import lz.controls.Alert;

	/**
	 *宝库 item 
	 * @author Administrator
	 * 
	 */	
	public class TreasuryItem extends Sprite implements IComponent
	{
		/**
		 * 
		 */
		private var _skin:MovieClip;
		/**
		 *购买按钮  
		 */
		private var buyBtn:SimpleButton;
		/**
		 * 
		 */
		private var _goodsVo:GoodsVo;
		private var _tyep:int;
		private var _goodItem:GoodItem;
		
		public function TreasuryItem()
		{
			super();
		}
		
		public function initUI(view:Object=null):void
		{
			_skin = Reflection.createInstance("com.lz.modul.compete.treasuryItem");
			buyBtn = _skin.buyBtn
			this.addChild(_skin);
			_goodItem = new GoodItem();
			_goodItem.initUI();
			_skin.contain.addChild(_goodItem);
			addEvtListener();
		}
		/**
		 * 
		 * @param $arr
		 * 
		 */
		public function setdata($arr:Array):void
		{
			_goodsVo= AppData.getInstance().goodsManager.getGoodsByTypeAndId($arr[1],$arr[0]);
			if(_goodsVo != null)
			{
				_skin.cost.text = _goodsVo.cashpoint + "";
				_skin.goodsName.text = _goodsVo.name + "";
				_goodItem.data = _goodsVo;
			}
		}
		public function addEvtListener():void
		{
			buyBtn.addEventListener(MouseEvent.CLICK,onBuyHandler);
		}
		
		protected function onBuyHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			
			if(_goodsVo == null)return;
			if(AppData.getInstance().clubDatamanager.clubInfo.cashpoint < _goodsVo.cashpoint)
			{
//				Alert.showErrorTip("点券不足");
				
				Alert.noCashAlert("现有点券不足" + _goodsVo.cashpoint  + "点，是否前往充值？");
				return;
			}
			Alert.show("确定花" +_goodsVo.cashpoint + "购买" + _goodsVo.name,"",Alert.NO,GlobalDef.AlertContainer,onIsBuy);
		
				
		}
		private function onIsBuy($type:int):void
		{
			if($type == Alert.YES)
				AppEventDispatcher.getInstance().dispatchEvent(new CompeteEvent(CompeteEvent.BUYTREASURY,{type:_tyep,pid:_goodsVo.gid,ptype:_goodsVo.type}));		
		}
		
		public function removeEvtListener():void
		{
			buyBtn.removeEventListener(MouseEvent.CLICK,onBuyHandler);
		}
		
		public function destroyView():void
		{
			removeEvtListener();
			if(this.parent)
				this.parent.removeChild(this);
			_goodItem.destroyView();
			_goodItem= null;
		}

		/**
		 *类型
		 * 1为国王，2为大将 3 为勇士 
		 */
		public function get tyep():int
		{
			return _tyep;
		}

		/**
		 * @private
		 */
		public function set tyep(value:int):void
		{
			_tyep = value;
		}

	}
}